﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GS.Cricket.Service.Impl.NHibernate;
using NHibernate;
using NHibernate.Criterion;

namespace GS.Cricket.Service.Impl
{
    public class MatchService : IMatchService
    {

        #region IMatchService Members

        public int Add(Entities.Match match)
        {
            return (int)match.Add<Entities.Match>();
        }

        public void Update(Entities.Match match)
        {
            match.Update<Entities.Match>();
        }

        public void Delete(int matchId)
        {
            var existing = (Entities.Match)matchId.GetById<Entities.Match>();
            if (existing != null)
            {
                existing.Delete<Entities.Match>();
            }
        }

        public Entities.Match GetById(int matchId)
        {
            return (Entities.Match)matchId.GetById<Entities.Match>();
        }

        public IEnumerable<Entities.Match> GetByDate(DateTime date)
        {
            using (ISession session = NHibernateHelper<Entities.Match>.OpenSession())
            {
                var result = session
                    .CreateCriteria(typeof(Entities.Match))
                    .Add(Restrictions.Eq("DateTime", date))
                    .List<Entities.Match>();
                return result;
            }
        }

        public IEnumerable<Entities.Match> GetAllMatches()
        {
            return this.GetAll<Entities.Match>();
        }

        #endregion
    }
}
